﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Security.Cryptography;
using System.Web.Security;

using System.Globalization;

namespace RTGS.Web.support
{

    public class JSONHelper
    {
        public bool IsSuccess
        {
            get;
            set;
        }

        public string Message
        {
            get;
            set;
        }

        public object Data
        {
            get;
            set;
        }
    }

    public class Helper
    {
        public class Constanta
        {
            public const string MENU_ID = "menu_id";

            public const string USER_ID = "user_id";

        }

        public class String
        {
            public static string PropCase(string strText)
            {
                return new CultureInfo("en").TextInfo.ToTitleCase(strText.ToLower());
            }
            public static string PropCaseStartLower(string strText)
            {
                string retVal = PropCase(strText);
                retVal = retVal.Substring(0, 1).ToLower() + retVal.Substring(1, (retVal.Length - 1));
                return retVal;
            }

            public static string Left(string param, int length)
            {
                string result = param.Substring(0, length);
                return result;

            }

            public static string Right(string param, int length)
            {
                string result = param.Substring(param.Length - length, length);
                return result;

            }

            public static string Mid(string param, int startindex, int length)
            {
                string result = param.Substring(startindex, length);
                return result;
            }

            public static string Mid(string param, int startindex)
            {
                string result = param.Substring(startindex);
                return result;
            }

            private static string RepeatedCharacters(char myCharacter, int repetitions)
            {
                string result = "";
                return result.PadLeft(repetitions, myCharacter);
            }

            public static string Digits(string str, int len)
            {
                string result = str;
                if (len > str.Length)
                {
                    result = RepeatedCharacters('0', len - str.Length) + str;
                }
                return result;
            }

        }

        public enum Culture
        {
            English,
            Indonesian
        }

        public static string GetAppVirtualPath(System.Web.UI.Page page)
        {
            int port = page.Request.Url.Port;
            string baseServer = page.Request.Url.Host + ":" + port.ToString();
            string url = "http://" + baseServer + System.Web.HttpRuntime.AppDomainAppVirtualPath;

            return url;

        }

        public static string CreatePasswordHash(string pwd)
        {
            //Generate a cryptographic random number.
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
            byte[] buff = new byte[5];
            rng.GetBytes(buff);

            // Return a Base64 string representation of the random number.
            string salt = Convert.ToBase64String(buff);

            //string saltAndPwd = String.Concat(pwd, salt);

            string saltAndPwd = pwd;

            string hashedPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(saltAndPwd, "sha1");

            return hashedPwd;
        }

        public static DateTime FormatDate(string value, Culture culture)
        {
            DateTime dt;
            if (culture == Culture.Indonesian)
                dt = Convert.ToDateTime(value, CultureInfo.GetCultureInfo("id-ID").DateTimeFormat);
            else
                dt = Convert.ToDateTime(value, CultureInfo.GetCultureInfo("en-US").DateTimeFormat);

            return dt;
        }

        public static string ToMonthDayYear(string value)
        {
            string day = String.Left(value, 2);
            string month = String.Mid(value, 3, 2);
            string year = String.Right(value, 4);

            return month + "/" + day + "/" + year;

        }

        public static string ToSQLServerDateISO(string value)
        {
            string day = String.Left(value, 2);
            string month = String.Mid(value, 3, 2);
            string year = String.Right(value, 4);

            return year + month +day;

        }

        public static bool IsNumber(string val, System.Globalization.NumberStyles numberStyle, string cultureName)
        {
            Double result;
            return Double.TryParse(val, numberStyle, new System.Globalization.CultureInfo(cultureName), out result);

        }


    }

}
